/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-22
 * Time: 22:59
 */

import java.io.*;
import java.util.Scanner;
import java.util.SplittableRandom;

/**
 * 扫描指定目录，并找到名称或者内容中包含指定字符的所有普通文件
 * （不包含目录）
 */
public class Demo3 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要搜索的目录路径 ");
        String srcPath = scanner.next();
        System.out.println("请输入要查找的字符");
        String searchWord = scanner.next();
        File srcFile = new File(srcPath);
        if(!srcFile.isDirectory()){
            return;
        }
        search(srcFile,searchWord);

    }
    public static  void search(File srcFile ,String searchWord) throws FileNotFoundException {
        File[]   files = srcFile.listFiles();
        if(files==null){
            return;
        }
        for (File f: files) {
            if (f.isFile()) {
                String ss = f.getName();
                if (ss.contains(searchWord)) {
                    System.out.println(f.getAbsolutePath());
                    return;
                }
                matchWord(f, searchWord);
            }else {



        }
    }
    public static  void matchWord(File f , String searchWord){


                    try(Reader reader = new FileReader(f)){
                        StringBuilder stringBuilder = new StringBuilder();
                        while (true) {
                            int n = reader.read();
                            if (n == -1) {
                                break;
                            }
                            stringBuilder.append((char) n);


                        }
                        if (stringBuilder.indexOf(searchWord) >= 0) {
                            System.out.println(f.getAbsolutePath());
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }

            }

        }

}
